package com.itheima.ai.controller;

import com.itheima.ai.service.ChatHistoryService;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;

import static org.springframework.ai.chat.client.advisor.AbstractChatMemoryAdvisor.CHAT_MEMORY_CONVERSATION_ID_KEY;

@RestController
@RequestMapping("/ai")
public class CustomerServiceController {


    @Autowired
    private ChatClient serviceChatClient;


    @Autowired
    private ChatHistoryService chatHistoryService;


    @GetMapping(value = "/service", produces = "text/html;charset=UTF-8")
    public Flux<String> service(@RequestParam(value = "prompt") String prompt, String chatId) {
        chatHistoryService.saveHistory("service",chatId);
        Flux<String> contents = serviceChatClient.prompt()
                .user(prompt)
                .advisors(advisorSpec -> advisorSpec.param(CHAT_MEMORY_CONVERSATION_ID_KEY,chatId))
                .stream()
                .content();
        return contents;
    }




}
